<?php

/**
 * This is the model class for table "user_photos".
 *
 * The followings are the available columns in table 'user_photos':
 * @property string $photos_id
 * @property integer $user_id
 * @property string $link_img
 * @property integer $approve
 * @property string $text
 * @property integer $make_primary
 */
class mUserPhotos extends CActiveRecord {

  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return mUserPhotos the static model class
   */
  public $file_upload = '';

  public static function model($className = __CLASS__) {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'user_photos';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('file_upload', 'file', 'types' => 'JPG,PNG,TIF,BMP,GIF', 'maxSize' => 5242880, 'tooLarge' => 'File has to be smaller than 5MB', 'on' => 'upload1'),
        //array('file_upload','email','on'=>'upload'),
        //array('file_upload','required','on'=>'upload'),
        array('photos_id, user_id, link_img', 'required', 'on' => 'insert,update'),
        array('user_id, approve, make_primary', 'numerical', 'integerOnly' => true, 'on' => 'insert,update'),
        array('photos_id', 'length', 'max' => 20, 'on' => 'insert,update'),
        array('link_img', 'length', 'max' => 50, 'on' => 'insert,update'),
        array('text', 'length', 'max' => 30, 'on' => 'insert,update'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('photos_id, user_id, link_img, approve, text, make_primary', 'safe', 'on' => 'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
        'photos_id' => 'Photos',
        'user_id' => 'User',
        'link_img' => 'Link Img',
        'approve' => 'Approve',
        'text' => 'Text',
        'make_primary' => 'Make Primary',
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search() {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria = new CDbCriteria;

    $criteria->compare('photos_id', $this->photos_id, true);
    $criteria->compare('user_id', $this->user_id);
    $criteria->compare('link_img', $this->link_img, true);
    $criteria->compare('approve', $this->approve);
    $criteria->compare('text', $this->text, true);
    $criteria->compare('make_primary', $this->make_primary);

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
  }

  public function getPhotos($user_id) {
    $photos = Yii::app()->db->createCommand()
            ->select('*')
            ->from('user_photos up')
            ->where('up.user_id=:user_id and up.approve < 2', array(':user_id' => $user_id))
            ->queryAll();
    return $photos;
  }

  public function getPhotosPrimary($user_id) {
    $photos = Yii::app()->db->createCommand()
            ->select('*')
            ->from('user_photos up')
            ->where('up.user_id=:user_id and up.approve < 2 and make_primary = 1', array(':user_id' => $user_id))
            ->queryRow();
    return $photos;
  }

  public function getPhotosRequestPrimary($user_id) {
    $photos = Yii::app()->db->createCommand()
            ->select('*')
            ->from('user_photos up')
            ->where('up.user_id=:user_id and up.approve < 2 and request = 1', array(':user_id' => $user_id))
            ->queryRow();
    return $photos;
  }

  public function getPhotosAlbum($user_id) {
    $photos = Yii::app()->db->createCommand()
            ->select('*')
            ->from('user_photos up')
            ->where('up.user_id=:user_id and up.approve < 2 and make_primary = 0 and request = 0', array(':user_id' => $user_id))
            ->queryAll();
    return $photos;
  }

  public function getAllPhotosWithUserLike($user_id, $user_like) {
    $link_img = FRONT_SITE_URL . 'www/uploads/photos/images/';
    $photos = Yii::app()->db->createCommand()
            ->select("up.photos_id MediaId, CONCAT('{$link_img}',up.link_img) as FullSizeUrl, pl.like Liked")
            ->from('user_photos up')
            ->leftJoin('photos_like pl', 'pl.photos_id=up.photos_id and pl.like = 1 AND pl.user_id=:user_id', array(':user_id' => $user_like))
            ->where('up.user_id=:user_id and up.approve < 2', array(':user_id' => $user_id))
            ->queryAll();
    return $photos;
  }

  public static function savePhotos($fileupload, $nameFile) {
    $fileupload->saveAs(UPLOAD_PATH . 'photos/images/' . $nameFile);
  }

  public function insertUserPhotos($user_id) {
    $command = db()->createCommand();
    $command->insert('user_photos', array(
        'user_id' => $user_id
    ));
  }

  public function updateUserPhotos($photos_id, $link_img) {
    $command = db()->createCommand();
    $command->update('user_photos', array(
        'link_img' => $link_img,
            ), 'photos_id=:photos_id', array(':photos_id' => $photos_id));
  }

  public function updateRequestPrimary($photos_id, $user_id) {
    $command = db()->createCommand();
    $command->update('user_photos', array(
        'make_primary' => 0,
        'request' => 0,
        'can_primary' => 0,
            ), 'user_id=:user_id', array(':user_id' => $user_id));

    $command->update('user_photos', array(
        'request' => 1,
            ), 'photos_id=:photos_id', array(':photos_id' => $photos_id));
  }

}